package com.blb.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.entity.Logistics;
import com.blb.entity.User;
import com.blb.entity.page.BlbPage;
import com.blb.entity.page.DataTablePage;
import com.blb.service.ILogisticsService;
import com.blb.utils.ResponseHelp;
import com.blb.utils.ShiroUtils;
import com.blb.utils.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author HuaHao
 * @since 2019-11-16
 */

@Controller
@RequestMapping("/logisticsxx")
public class LogisticsController {

    @Autowired
    private ILogisticsService iLogisticsService;



    @RequestMapping("{page}")
    public String wel(@PathVariable String page){
        return "logistics/"+ page;
    }

    /*显示所有商品*/
    @ResponseBody
    @RequestMapping("findAll")
    public BlbPage findAllProduct(@RequestParam(required = false, value ="logisticsName") String logisticsName, Integer start, Integer length){
        System.out.println(logisticsName);
        User user = ShiroUtils.getUser();
        String companyId = user.getCompanyId();
        /*查询分页数据*/
        IPage page = iLogisticsService.page(new Page(start, length), new QueryWrapper<Logistics>().like("logistics_name", logisticsName).eq("company_id",companyId));
        System.out.println(page.getRecords());
        /*查询总条数*/
        int count = iLogisticsService.count(new QueryWrapper<Logistics>().like("logistics_name", logisticsName));
        return new DataTablePage(count,count,page.getRecords());
    }




    @ResponseBody
    @RequestMapping("findbyid")
    public ResponseHelp  findRemarksById(String logisticsId){
        String remarksById = iLogisticsService.findRemarksById(logisticsId);
        ResponseHelp responseHelp =new ResponseHelp();
        responseHelp.setData(remarksById);
        System.out.println(responseHelp);
        return responseHelp;
    }

    @ResponseBody
    @RequestMapping("add")
    public ResponseHelp addLogistics(Logistics logistics) throws ParseException {
        User user = ShiroUtils.getUser();
        String companyId = user.getCompanyId();
        logistics.setLogisticsId(UUIDUtils.getId());
        String userId = user.getId();


        //获取当前时间
        logistics.setJoinTime(new Date());
        logistics.setCreateTime(new Date());
        logistics.setCompanyId(companyId);
        logistics.setCreateId(userId);
        iLogisticsService.save(logistics);

        System.out.println(logistics);
        System.out.println("到了添加步骤");

        ResponseHelp responseHelp =new ResponseHelp();
        return responseHelp;
    }

    @ResponseBody
    @RequestMapping("delete")
    public ResponseHelp deleteLogistics(String id){
        System.out.println(id);
        ResponseHelp responseHelp =new ResponseHelp();
        iLogisticsService.deleteLogistics(id);
        return responseHelp;
    }

    @ResponseBody
    @RequestMapping("update")
    public ResponseHelp update(Logistics logistics){
        User user = ShiroUtils.getUser();
        String userId = user.getId();
        logistics.setUpdateId(userId);
        logistics.setUpdateTime(new Date());
        iLogisticsService.update(logistics);
        ResponseHelp responseHelp =new ResponseHelp();
        return responseHelp;
    }

    @ResponseBody
    @RequestMapping("alllogistics")
    public ResponseHelp allLogistics(){
        User user = ShiroUtils.getUser();
        String companyId = user.getCompanyId();
        List<Logistics> list = iLogisticsService.list(new QueryWrapper<Logistics>().eq("company_id", companyId));
        System.out.println(list);
        ResponseHelp responseHelp =new ResponseHelp();
        responseHelp.setData(list);
        return responseHelp;
    }
}

